package com.dindang.checkline.mvp.greendao.controller;

import com.dindang.checkline.app.utils.DBUtils;
import com.dindang.checkline.app.utils.MyUtils;
import com.dindang.checkline.mvp.greendao.entity.CheckPiont;
import com.dindang.checkline.mvp.greendao.gen.CheckPiontDao;
import com.longxi.gamer_base.BaseAppConst;

import java.util.List;

/**
 * Created by cxw on & 2018/11/9
 */
public class CheckPiontController {

    private static CheckPiontDao checkPiontDao = DBUtils.getDaoSession().getCheckPiontDao();

    /**
     * 增加巡检点
     */
    public static long insertPiont(CheckPiont piont,Long lineId) {
        piont.setUserId(BaseAppConst.accountInfo.getUserId()+"");
        piont.setLineId(lineId);
        return checkPiontDao.insertOrReplace(piont);
    }

    /**
     * 删除所有巡检点
     */
    public static void deleteAllPiont(String lineId) {
        List<CheckPiont> list = checkPiontDao.queryBuilder()
                .where(CheckPiontDao.Properties.UserId.eq(BaseAppConst.accountInfo.getUserId()))
                .where(CheckPiontDao.Properties.LineId.eq(lineId))
                .list();
        checkPiontDao.deleteInTx(list);
    }

    /**
     * 查巡检点列表根据巡检路线id
     */
    public static List<CheckPiont> selectByLineid(String lineId)
    {
        //惰性加载
        List<CheckPiont> list = checkPiontDao.queryBuilder()
                .where(CheckPiontDao.Properties.UserId.eq(BaseAppConst.accountInfo.getUserId()))
                .where(CheckPiontDao.Properties.LineId.eq(lineId))
                .list();
        return list;
    }

    /**
     * 更新一条数据
     * @param item
     */
    /**
     * 更新状态
     */
    public static void changeStatus(CheckPiont item){
        List<CheckPiont> list = checkPiontDao.queryBuilder()
                .where(CheckPiontDao.Properties.UserId.eq(BaseAppConst.accountInfo.getUserId()))
                .where(CheckPiontDao.Properties.LId.eq(item.getlId()))
                .list();
        if(list!=null&&list.size()>0){
            list.get(0).setcStatus(item.getcStatus());
            list.get(0).setRItemLastTime(item.getRItemLastTime());
            checkPiontDao.update(list.get(0));
        }
    }

    /**
     * 根据步骤查询一条记录
     */
//    public static item findByBillidAndStep(String billid, String step)
//    {
//        //惰性加载
//        item item = mCheckItemDao.queryBuilder()
//                .where(CheckItemDao.Properties.Userid.eq(Long.parseLong(MyUtils.userid)))
//                .where(CheckItemDao.Properties.Billid.eq(billid))
//                .where(CheckItemDao.Properties.Step.eq(step))
//                .unique();
//        return item;
//    }
}
